package com.cliff.joinus.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.cliff.joinus.Constants;
import com.cliff.joinus.MainApplication;
import com.cliff.joinus.R;
import com.cliff.joinus.activity.fragment.LocalMediaFragment;
import com.cliff.joinus.activity.fragment.MediaServerFragment;
import com.cliff.joinus.activity.fragment.RendererFragment;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends SherlockFragmentActivity {
	
	protected final int[][] ACTIONBARMENU = new int[][]{Constants.AB_QUIT, Constants.AB_SERVER, Constants.AB_PREFERENCE, Constants.AB_INFO};

    protected final int[] TABNAMES = new int[] {
            R.string.tab_renderer,
            R.string.tab_local,
            R.string.tab_network,
//            R.string.tab_internet,
    };
    
    protected final int[] TABICONS = new int[] {
        R.drawable.tab_renderer,
        R.drawable.tab_local,
        R.drawable.tab_network,
//        R.drawable.tab_internet,
    };
    
    protected final int DEFAULT_ITEM = 1;

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
//		((MainApplication)getApplication()).init();
		
        FragmentPagerAdapter adapter = new MainActivityAdapter(getSupportFragmentManager());

        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
        indicator.setViewPager(pager);
        indicator.setCurrentItem(DEFAULT_ITEM);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		((MainApplication)getApplication()).uninit();
		super.onDestroy();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		initMenu(menu, ACTIONBARMENU);

		// return super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (Constants.GROUP_COMMON == item.getGroupId()) {
			switch(item.getItemId()) {
			case Constants.ITEM_INFO:
				startActivity(new Intent(this, InfoActivity.class));
				break;
			case Constants.ITEM_SERVER:
			case Constants.ITEM_PREFERENCE:
				break;
			case Constants.ITEM_QUIT:
				promptQuit();
				break;
			}
		}
		
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		promptQuit();
	}

	protected void promptQuit() {
		new AlertDialog.Builder(this)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setTitle(R.string.title_dialog_quit)
		.setMessage(R.string.hint_really_quit)
		.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						// Stop the activity
						MainActivity.this.finish();
					}
				}).setNegativeButton(R.string.no, null).show();
	}

    class MainActivityAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
        public MainActivityAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        	//test code
        	switch(position) {
//        	case 3:
//                return TestFragment.newInstance(getString(TABNAMES[position % TABNAMES.length]));
        	case 0:
            	return new RendererFragment();
        	case 1:
            	return new LocalMediaFragment();
        	case 2:
            	return new MediaServerFragment();
        	}
			return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return getString(TABNAMES[position % TABNAMES.length]).toUpperCase();
        }

        @Override
        public int getCount() {
          return TABNAMES.length;
        }

        @Override public int getIconResId(int index) {
            return TABICONS[index];
        }
    }
    
    public static void initMenu(Menu menu, int[][] menuData) {
		for (int i = 0; i < menuData.length; i++) {
			int item[] = menuData[i];
			menu.add(Constants.GROUP_COMMON, item[0], Menu.NONE, item[2])
			.setIcon(item[1])
			.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		}
    }
	
}
